package csu.softwaremetric.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import csu.softwaremetric.service.CKMetricService;
import csu.softwaremetric.service.ChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@Controller
public class CKMetricController {
    @Autowired
    private CKMetricService ckMetricService;

    @Autowired
    private ChatService chatService;

    @GetMapping(value = "/CKXml/{name}")
    public ResponseEntity<JSONArray> getCKMetric(@PathVariable String name)
    {
        String path = "upload/XML/" + name;
        JSONArray jsonArray = ckMetricService.CKMetricALL(path);
        if(jsonArray != null)
        {
            return new ResponseEntity<>(jsonArray, HttpStatus.OK);
        }
        else {
            jsonArray = new JSONArray();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("STATUS", "FAILURE");
            jsonObject.put("MSG", "xml文件不存在");
            jsonArray.add(jsonObject);
            return new ResponseEntity<>(jsonArray, HttpStatus.OK);
        }
    }

}
